package com.hundredapp.oa.web.service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaContract;
import com.hundredapp.oa.domain.OaElect;
import com.hundredapp.oa.domain.OaSubject;
import com.hundredapp.oa.repository.ClassmngManager;
import com.hundredapp.oa.repository.ElectManager;
import com.hundredapp.oa.repository.SignManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.repository.SubjectManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.HadSubjectVO;
import com.hundredapp.oa.vo.SubjectVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Subject extends ActionSupport {
	@Autowired
	private SubjectManager subjectManager;
	@Autowired
	private StudentManager studentmng;
	@Autowired
	private ElectManager electmng;
	@Autowired
	private SignManager signmng;
	@Autowired
	private ClassmngManager classmng;
	
	private Integer studentid;

	private Object success;

	private Integer subjectid;

	private String msg;

	private List<OaSubject> root;
	
	private List<SubjectVO> subvos;
	
	private List<HadSubjectVO> hadsubvos;
	
	private SubjectVO subjectvo;

	private OaSubject subject;

	private String query;

	private int limit;

	private int start;

	private int total;

	public String allpre() {
		root = subjectManager.findAll(SelectType.SELECT_ALL);
		return SUCCESS;
	}
	
	public String allsub() {
		String condition = SelectType.SELECT_ALL;

		if (query != null) {
			query = JSEscape.unescape(query);
			condition += " and displayName like '%" + query + "%' ";
		}

		root = subjectManager.find(condition, start, limit);
		subvos = new ArrayList<SubjectVO>();
		for(OaSubject sub:root){
			SubjectVO subvo = new SubjectVO();
			subvo.setId(sub.getId());
			subvo.setDescription(sub.getDescription());
			subvo.setDisplayName(sub.getDisplayName());
			subvo.setLesson(sub.getLesson());
			switch(sub.getSemester()){
			case 1: subvo.setSemesters("第一学期"); break;
			case 2: subvo.setSemesters("第二学期"); break;
			case 3: subvo.setSemesters("第三学期"); break;
			case 4: subvo.setSemesters("第四学期"); break;
			default: subvo.setSemesters("第一学期"); break;
			}
			subvo.setPrice(sub.getPrice());
			subvos.add(subvo);
		}
		total = subjectManager.count(condition);
		return SUCCESS;
	}
	
	public String findbyid(){
		subject = subjectManager.findById(subjectid);
		return SUCCESS;
	}

	public String add() {
		subject.setCreateUserId(WebUtil.GetLoginUserId());
		subjectManager.add(subject);
		success = true;
		msg = "更新成功";
		return SUCCESS;
	}

	public String delete() {
		try {
			subjectManager.delete(subjectid);
		} catch (Exception e) {
			success = false;
			msg = "有学生正在选修这门课程。<br/><br/>请清空后删除。";
			return SUCCESS;
		}
		success = true;
		msg = "删除成功";
		return SUCCESS;
	}
	
	public String findbystudent(){
		subvos = new ArrayList<SubjectVO>();
		
		List<OaContract> contracts = studentmng.findById(studentid).getOaContracts();
		for(OaContract contract:contracts){
			List<OaElect> elects = electmng.findByContract(contract.getId());
			for(OaElect elect:elects){
				if(elect.getIspre() == 1){
					OaSubject subject = elect.getOaSubject();
					SubjectVO subvo = new SubjectVO();
					subvo.setId(subject.getId());
					subvo.setDisplayName(subject.getDisplayName());
					subvo.setDescription(subject.getDescription());
					double price = subject.getPrice();
					double lePrice = price/subject.getLesson();
					Integer lesson = (int) (elect.getPrice()/lePrice);
					subvo.setLesson(lesson);
					subvos.add(subvo);
				}
			}
		}
		return SUCCESS;
	}
	
	public String findhadbystu(){
		hadsubvos = new ArrayList<HadSubjectVO>();
		
		if (studentid>0) {
			List<OaContract> contracts = studentmng.findById(studentid).getOaContracts();
			for(OaContract contract:contracts){
				List<OaElect> elects = electmng.findByContract(contract.getId());
				for(OaElect elect:elects){
					OaSubject subject = elect.getOaSubject();
					HadSubjectVO hadsubvo = new HadSubjectVO();
					hadsubvo.setId(subject.getId());
					hadsubvo.setDisplayName(subject.getDisplayName());
					String semester = "";
					int intsemester = subject.getSemester();
					switch(intsemester){
						case 1: semester="第一学期";break;
						case 2: semester="第二学期";break;
						case 3: semester="第三学期";break;
						case 4: semester="第四学期";break;
						default: semester="第一学期";break;
					}
					hadsubvo.setSemester(semester);
					OaClass class1 = classmng.findBySub(subject.getId());
					hadsubvo.setClassName(class1.getClassName());
					Integer hadlesson = signmng.count(" studentId = "+studentid+" and classId="+class1.getId());
					hadsubvo.setLesson(hadlesson);
					hadsubvos.add(hadsubvo);
				}
			}
		}
		return SUCCESS;
	}

	public List<OaSubject> getRoot() {
		return root;
	}

	public void setSubjectManager(SubjectManager subjectManager) {
		this.subjectManager = subjectManager;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public OaSubject getSubject() {
		return subject;
	}

	public void setSubject(OaSubject subject) {
		this.subject = subject;
	}

	public void setSubjectid(Integer subjectid) {
		this.subjectid = subjectid;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getTotal() {
		return total;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setStudentid(Integer studentid) {
		this.studentid = studentid;
	}

	public List<SubjectVO> getSubvos() {
		return subvos;
	}

	public void setSubvos(List<SubjectVO> subvos) {
		this.subvos = subvos;
	}

	public List<HadSubjectVO> getHadsubvos() {
		return hadsubvos;
	}

	public void setHadsubvos(List<HadSubjectVO> hadsubvos) {
		this.hadsubvos = hadsubvos;
	}

	public SubjectVO getSubjectvo() {
		return subjectvo;
	}

	public void setSubjectvo(SubjectVO subjectvo) {
		this.subjectvo = subjectvo;
	}
}
